﻿using Kvaser.CanLib;

namespace CANReceiveDemo
{
    internal class Program
    {
        private static int count = 0;
        static void Main(string[] args)
        {
            //初始化Can对象
            Canlib.canInitializeLibrary();
            //打开通道，获取句柄
            int handle = Canlib.canOpenChannel(1, Canlib.canOPEN_ACCEPT_VIRTUAL);
            //通过句柄设置波特率
            Canlib.canSetBitrate(handle, Canlib.canBITRATE_500K);
            //打开连接
            Canlib.canBusOn(handle);
            while (count < 100)
            {
                Thread.Sleep(1000);
                //接受
                byte[] dates = new byte[8];
                Canlib.canRead(handle, out int id, dates, out int dlc, out int flag, out long time).ToString();
                Console.WriteLine($"Receive : {BitConverter.ToString(dates)}, Id : {id.ToString()}");
                
                count++;
            }
            Canlib.canClose(handle);
            Console.WriteLine("按任意键退出......");
            Console.ReadKey();
        }
    }
}
